//
//  UIView+Gradient.m
//  kongfangxiong
//
//  Created by 李小龙 on 2017/6/26.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "UIView+Gradient.h"

@implementation UIView (Gradient)

- (void)setGradient:(NSArray *)colorArr startP:(CGPoint)startP endP:(CGPoint)endP bgView:(UIView *)bgView {
    if (!colorArr) {
        colorArr = @[[UIColor colorWithHex:0xFF4C72 alpha:1],[UIColor colorWithHex:0xFF690F alpha:1]];
    }
    
    // 初始化layer
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    // 渐变色数组
    NSMutableArray *gradientColors = [[NSMutableArray alloc] init];
    for (UIColor *color in colorArr) {
        if ([color isKindOfClass:[UIColor class]]) {
            [gradientColors addObject:(__bridge id)color.CGColor];
        }
    }
    gradientLayer.colors = gradientColors;
    // 起始 结束 位置
    gradientLayer.startPoint = startP;
    gradientLayer.endPoint = endP;
    
    for (CAGradientLayer *subLayer in self.layer.sublayers) {
        if ([subLayer isKindOfClass:[CAGradientLayer class]]) {
            [subLayer removeFromSuperlayer];
            break;
        }
    }
    
    if (bgView) {
        // layer frame
        gradientLayer.frame = self.frame;
        
        [bgView.layer insertSublayer:gradientLayer atIndex:0];
        gradientLayer.mask = self.layer;
        self.frame = gradientLayer.bounds;
    } else {
        gradientLayer.frame = CGRectMake(0, 0, self.mj_w, self.mj_h);
        
        [self.layer insertSublayer:gradientLayer atIndex:0];
    }
}

- (void)setGradientHorizontal:(NSArray *)colorArr {
    if (!colorArr) {
        colorArr = @[[UIColor colorWithHex:0xFF4C72 alpha:1],[UIColor colorWithHex:0xFF690F alpha:1]];
    }
    
    // 初始化layer
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    // 渐变色数组
    NSMutableArray *gradientColors = [[NSMutableArray alloc] init];
    for (UIColor *color in colorArr) {
        if ([color isKindOfClass:[UIColor class]]) {
            [gradientColors addObject:(__bridge id)color.CGColor];
        }
    }
    gradientLayer.colors = gradientColors;
    // 渐变范围
    CGFloat average = 1.0 / gradientColors.count;
    NSMutableArray *gradientLocations = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < gradientColors.count; i++) {
        [gradientLocations addObject:[NSNumber numberWithFloat:average*i]];
    }
    gradientLayer.locations = gradientLocations;
    // 起始 结束 位置
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    // layer frame
    gradientLayer.frame = CGRectMake(0, 0, self.mj_w, self.mj_h);
    
    for (CAGradientLayer *subLayer in self.layer.sublayers) {
        if ([subLayer isKindOfClass:[CAGradientLayer class]]) {
            [subLayer removeFromSuperlayer];
            break;
        }
    }
    [self.layer insertSublayer:gradientLayer atIndex:0];
}

- (void)setGradientVertical:(NSArray *)colorArr {
    if (!colorArr) {
        colorArr = @[[UIColor colorWithHex:0xFF4C72 alpha:1],[UIColor colorWithHex:0xFF690F alpha:1]];
    }
    // 初始化layer
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    // 渐变色数组
    NSMutableArray *gradientColors = [[NSMutableArray alloc] init];
    for (UIColor *color in colorArr) {
        if ([color isKindOfClass:[UIColor class]]) {
            [gradientColors addObject:(__bridge id)color.CGColor];
        }
    }
    gradientLayer.colors = gradientColors;
    // 渐变范围
    CGFloat average = 1.0 / gradientColors.count;
    NSMutableArray *gradientLocations = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < gradientColors.count; i++) {
        [gradientLocations addObject:[NSNumber numberWithFloat:average*i]];
    }
    gradientLayer.locations = gradientLocations;
    // 起始 结束 位置
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1.0);
    // layer frame
    gradientLayer.frame = CGRectMake(0, 0, self.mj_w, self.mj_h);
    
    [self.layer addSublayer:gradientLayer];
}

- (void)setGradientDiagonal:(NSArray *)colorArr {
    if (!colorArr) {
        colorArr = @[[UIColor colorWithHex:0xFF4C72 alpha:1],[UIColor colorWithHex:0xFF690F alpha:1]];
    }
    // 初始化layer
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    // 渐变色数组
    NSMutableArray *gradientColors = [[NSMutableArray alloc] init];
    for (UIColor *color in colorArr) {
        if ([color isKindOfClass:[UIColor class]]) {
            [gradientColors addObject:(__bridge id)color.CGColor];
        }
    }
    gradientLayer.colors = gradientColors;
    // 渐变范围
    CGFloat average = 1.0 / gradientColors.count;
    NSMutableArray *gradientLocations = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < gradientColors.count; i++) {
        [gradientLocations addObject:[NSNumber numberWithFloat:average*i]];
    }
    gradientLayer.locations = gradientLocations;
    // 起始 结束 位置
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 1.0);
    // layer frame
    gradientLayer.frame = CGRectMake(0, 0, self.mj_w, self.mj_h);
    
    [self.layer addSublayer:gradientLayer];
}
@end
